package com.lepu.app.main.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.app.utils.Setting;
import com.core.lib.adapter.ViewPageAdapter;
import com.core.lib.utils.OnZoomImageClickCallBack;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.photo.BitmapUtil;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.utils.viewpager.RotateDownPageTransformer;
import com.core.lib.utils.viewpager.ViewPagerCompat;
import com.core.lib.utils.viewpager.ViewPagerCompat.OnPageChangeListener;
import com.core.lib.widget.AnimationImageView;
import com.eyzhs.app.R;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

public class AppImageBroswerActivity extends Activity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener {

	public ViewPagerCompat mViewPager;
	private LinearLayout mFocusIndicator;

	private String[] mUrlArray = null;
	private String[] mFilePathArray = null;

	private ViewPageAdapter mAdapter = null;
	private ArrayList<View> mViewList = new ArrayList<View>();

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_image_broswer_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish();
	}

	/**
	 * 初始化方法
	 */
	private void init() {

		boolean isShowTopbar = false;
		int index = 0;
		Bundle data = getIntent().getExtras();
		if (data != null && data.size() > 0) {
			isShowTopbar = data.getBoolean("isShowTopbar");
			mUrlArray = data.getStringArray("urlArray");
			mFilePathArray = data.getStringArray("filePathArray");
			index = data.getInt("index");
		}
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);

		if (isShowTopbar) {
			topbar.setTopbarTitle("");
			topbar.setRightText("删除");
			topbar.setonTopbarNewLeftLayoutListener(this);
			topbar.setOnTopbarNewRightButtonListener(this);
		} else {
			topbar.setVisibility(View.GONE);
		}

		mViewPager = (ViewPagerCompat) findViewById(R.id.viewPager);
		mViewPager.setPageTransformer(true, new RotateDownPageTransformer());
		mFocusIndicator = (LinearLayout) findViewById(R.id.focusIndicator);

		int count = 0;
		if (mUrlArray != null && mUrlArray.length > 0) {
			count = mUrlArray.length;
		} else {
			if (mFilePathArray != null && mFilePathArray.length > 0) {
				count = mFilePathArray.length;
			}
		}

		for (int i = 0; i < count; i++) {
			View v = new View(this);
			v.setBackgroundResource(R.drawable.app_point_bg);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					12, 12);
			layoutParams.setMargins(4, 4, 4, 4);
			v.setEnabled(false);

			if (i == index) {
				v.setEnabled(true);
			}
			mFocusIndicator.addView(v, layoutParams);
		}

		addView();

		mAdapter = new ViewPageAdapter(mViewList, null, null);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		mViewPager.setCurrentItem(index);
	}

	private void addView() {

		if (mUrlArray != null && mUrlArray.length > 0) {
			for (String url : mUrlArray) {
				final AnimationImageView imageView = new AnimationImageView(
						this);
				imageView.setLayoutParams(new ViewGroup.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT,
						ViewGroup.LayoutParams.MATCH_PARENT));
				imageView.setAdjustViewBounds(true);
				imageView.setScaleType(ScaleType.FIT_CENTER);
				imageView
						.setClickCallBack(new OnZoomImageClickCallBack() {

							@Override
							public void OnSingleTapConfirmed(PointF point) {
								finish();
							}

							@Override
							public void OnLongClickResponse(PointF point) {

							}

							@Override
							public void OnDoubleClickResponse(PointF point) {
								float magfactor = 1.6f;
								float float_epsilon = 1e-3f;
								float viewwidth = imageView.getWidth();
								float viewheight = imageView.getHeight();
								float imagewidth = imageView.getDrawable()
										.getBounds().width();
								float imageheight = imageView.getDrawable()
										.getBounds().height();

								Matrix matrix = new Matrix();
								matrix.set(imageView.getImageMatrix());
								float[] values = new float[9];
								matrix.getValues(values);

								float ratiox = viewwidth / imagewidth;
								float ratioy = viewheight / imageheight;
								float minratio = ratiox < ratioy ? ratiox
										: ratioy;
								float transx, transy;
								// 回原图
								if (values[0] > (minratio + float_epsilon)) {
									if (ratiox < ratioy) {
										transx = 0;
										transy = (viewheight - ratiox
												* imageheight) / 2;
									} else {
										transx = (viewwidth - ratioy
												* imagewidth) / 2;
										transy = 0;
									}
									values[0] = values[4] = minratio;
									values[2] = transx;
									values[5] = transy;
									matrix.setValues(values);
									imageView.EnableAnimation(
											imageView.getImageMatrix(), matrix,
											200);
								} else {// 放大
									matrix.postScale(magfactor, magfactor,
											point.x, point.y);
									imageView.EnableAnimation(
											imageView.getImageMatrix(), matrix,
											200);
								}
							}
						});

				// load image
				String imageUrl = "";
				if (url.startsWith("http")) {
					imageUrl = url;
				} else {
					imageUrl = Setting.getImageUrl(url);
				}

				UniversalImageLoadTool.disPlay(imageUrl, imageView,
						R.drawable.default_icon_small);

				mViewList.add(imageView);
			}
		} else {
			if (mFilePathArray != null && mFilePathArray.length > 0) {
				for (String filePath : mFilePathArray) {
					final AnimationImageView imageView = new AnimationImageView(
							this);
					imageView.setLayoutParams(new ViewGroup.LayoutParams(
							ViewGroup.LayoutParams.MATCH_PARENT,
							ViewGroup.LayoutParams.MATCH_PARENT));

					imageView.setAdjustViewBounds(true);
					imageView.setScaleType(ScaleType.FIT_CENTER);
					imageView
							.setClickCallBack(new OnZoomImageClickCallBack() {

								@Override
								public void OnSingleTapConfirmed(PointF point) {
									finish();
								}

								@Override
								public void OnLongClickResponse(PointF point) {

								}

								@Override
								public void OnDoubleClickResponse(PointF point) {
									float magfactor = 1.6f;
									float float_epsilon = 1e-3f;
									float viewwidth = imageView.getWidth();
									float viewheight = imageView.getHeight();
									float imagewidth = imageView.getDrawable()
											.getBounds().width();
									float imageheight = imageView.getDrawable()
											.getBounds().height();

									Matrix matrix = new Matrix();
									matrix.set(imageView.getImageMatrix());
									float[] values = new float[9];
									matrix.getValues(values);

									float ratiox = viewwidth / imagewidth;
									float ratioy = viewheight / imageheight;
									float minratio = ratiox < ratioy ? ratiox
											: ratioy;
									float transx, transy;
									// 回原图
									if (values[0] > (minratio + float_epsilon)) {
										if (ratiox < ratioy) {
											transx = 0;
											transy = (viewheight - ratiox
													* imageheight) / 2;
										} else {
											transx = (viewwidth - ratioy
													* imagewidth) / 2;
											transy = 0;
										}
										values[0] = values[4] = minratio;
										values[2] = transx;
										values[5] = transy;
										matrix.setValues(values);
										imageView.EnableAnimation(
												imageView.getImageMatrix(),
												matrix, 200);
									} else {// 放大
										matrix.postScale(magfactor, magfactor,
												point.x, point.y);
										imageView.EnableAnimation(
												imageView.getImageMatrix(),
												matrix, 200);
									}
								}
							});

//					UniversalImageLoadTool.disPlay("file://" + filePath,
//							imageView, R.drawable.default_icon_small);
					
//					LocalImageLoader imageloader = LocalImageLoader.getInstance(this);
//					imageloader.disPlay(ImageUtilBase.getFileImageUrl(filePath), new RotateImageViewAware(imageView,
//							filePath), R.drawable.default_icon_small);
					
					Bitmap bitmap = ImageUtilBase.getBitmap(filePath,480,800);
					imageView.setImageBitmap(BitmapUtil.reviewPicRotate(bitmap, filePath));
					
					mViewList.add(imageView);
				}
			}
		}
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int position) {
			for (int i = 0; i < mFocusIndicator.getChildCount(); i++) {
				mFocusIndicator.getChildAt(i).setEnabled(false);
			}

			mFocusIndicator.getChildAt(position).setEnabled(true);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish();
	}

	@Override
	public void onTopbarRightButtonSelected() {

		// mViewList.remove(mViewPager.getCurrentItem());
		// mAdapter.notifyDataSetChanged();

		Intent intent = new Intent();
		intent.putExtra("index", mViewPager.getCurrentItem());
		setResult(Activity.RESULT_OK, intent);

		finish();
	}

}
